package com.snc.controller.system;

import com.snc.annotation.SystemLog;
import com.snc.controller.index.BaseController;
import com.snc.entity.ResFormMap;
import com.snc.entity.ResUserFormMap;
import com.snc.entity.RoleFormMap;
import com.snc.entity.UserRoleFormMap;
import com.snc.exception.SystemException;
import com.snc.plugin.PageView;
import com.snc.util.Common;
import com.snc.util.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author lanyuan 2014-11-19
 * @Email mmm333zzz520@163.com
 * @version 3.0v
 */
@Controller
@RequestMapping("/role/")
public class RoleController extends BaseController {

	@RequestMapping("listUI")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/system/role/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow,
			String pageSize) throws Exception {
		RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
		roleFormMap=toFormMap(roleFormMap, pageNow, pageSize);
        pageView=roleFormMap.findByPage(getPageView(pageNow, pageSize));
		return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		ResFormMap resFormMap = new ResFormMap();
		List<ResFormMap> mps = resFormMap.findByWhere();
		for (ResFormMap resFormMap2 : mps) {
			resFormMap2.remove("description");
			resFormMap2.remove("resUrl");
			resFormMap2.remove("resKey");
			resFormMap2.remove("icon");
		}
		model.addAttribute("permissions",JsonUtils.listToJson(mps));
		return Common.BACKGROUND_PATH + "/system/role/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="角色管理-新增角色")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(String resId,String txtUserSelect) throws Exception {
		try {
				RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
				roleFormMap.save();
				return pession(roleFormMap.get("id")+"", resId, txtUserSelect);
			} catch (Exception e) {
				//try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
				String results = "{\"results\":\"error\",\"message\":\""+e+"\"}";
				throw new SystemException(results);
			}
			
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="角色管理-删除角色")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity(String ids) throws Exception {
		RoleFormMap roleFormMap = new RoleFormMap();
		roleFormMap.deleteByAttribute("id", ids);
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			RoleFormMap roleFormMap = new RoleFormMap();
			roleFormMap.put("id", id);
			model.addAttribute("role", roleFormMap.findbyFrist());
		}
		ResFormMap resFormMap = new ResFormMap();
		List<ResFormMap> mps = resFormMap.findByWhere();
		for (ResFormMap resFormMap2 : mps) {
			resFormMap2.remove("description");
			resFormMap2.remove("resUrl");
			resFormMap2.remove("resKey");
			resFormMap2.remove("icon");
		}
		model.addAttribute("permissions",JsonUtils.listToJson(mps));
		return Common.BACKGROUND_PATH + "/system/role/edit";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="角色管理-修改角色")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(String resId,String txtUserSelect) throws Exception {
		try {
			RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
			roleFormMap.update();
			return pession(roleFormMap.get("id").toString(), resId, txtUserSelect);
		} catch (Exception e) {
			//try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
			String results = "{\"results\":\"error\",\"message\":\""+e+"\"}";
			throw new SystemException(results);
		}
	}
	
	public String pession(String roleId,String resId,String txtUserSelect) throws Exception{
		String[] txt = txtUserSelect.split(",");
		UserRoleFormMap userRoleFormMap = new UserRoleFormMap();
		if (!Common.isEmpty(txtUserSelect)) {
			userRoleFormMap.deleteByAttribute("roleId", roleId);;
			List<UserRoleFormMap> ulis = new ArrayList<UserRoleFormMap>();
			for (String userId : txt) {
				userRoleFormMap = new UserRoleFormMap();
				userRoleFormMap.put("userId", userId);
				userRoleFormMap.put("roleId", roleId);
				ulis.add(userRoleFormMap);
			}
			userRoleFormMap.batchSave(ulis);
		}
		if(Common.isNotEmpty(resId)){
			String[] users = txt;
			ResUserFormMap resUserFormMap = new ResUserFormMap();
			for (String uid : users) {
				String[] s = Common.trimComma(resId).split(",");
				List<ResUserFormMap> resUserFormMaps = new ArrayList<ResUserFormMap>();
				for (String rid : s) {
					resUserFormMap = new ResUserFormMap();
					resUserFormMap.put("resId", rid);
					resUserFormMap.put("userId", uid);
					resUserFormMap.deleteByNames();
					resUserFormMaps.add(resUserFormMap);
				}
				resUserFormMap.batchSave(resUserFormMaps);
			}
		}	
		return "success";
	}
	
	@RequestMapping("seletRole")
	public String seletRole(Model model) throws Exception {
		RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
		List<RoleFormMap> list = RoleFormMap.mapper().seletUserRole(roleFormMap);
		String ugid = "";
		for (RoleFormMap ml : list) {
			ugid += ml.get("id")+",";
		}
		ugid = Common.trimComma(ugid);
		model.addAttribute("txtRoleSelect", ugid);
		model.addAttribute("userRole", list);
		if(StringUtils.isNotBlank(ugid)){
			String[] g = ugid.split(",");
			String v = "";
			for (String s : g) {
				v+="'"+s+"',";
			}
			roleFormMap.put("where", " id not in ("+Common.trimComma(v)+")");
		}
		List<RoleFormMap> role = roleFormMap.findByWhere();
		model.addAttribute("role", role);
		return Common.BACKGROUND_PATH + "/system/role/roleSelect";
	}
	@RequestMapping("updateState")
	@ResponseBody
	@Transactional//需要事务操作必须加入此注解
	public String updateState() throws Exception {
		RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
		roleFormMap.update();
		return "success";
	}

}